package cn.fl.mcr.service.impl;

import java.util.Date;
import java.util.List;

import cn.cmm.base.framework.constant.BaseConstant;
import cn.fl.mcr.service.IMcrPrpsRateService;
import cn.fl.mcr.transform.McrPrpsRateVOTransform;
import cn.fl.mcr.vo.McrPrpsRateVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.condition.McrCityOpenCtrlQC;
import cn.lionbridgecapital.loan.model.dto.McrCityOpenCtrlDTO;
import cn.lionbridgecapital.loan.model.dto.McrPrpsRateDTO;
import cn.lionbridgecapital.loan.model.dto.McrPrpsRateDtlDTO;
import cn.lionbridgecapital.loan.model.exception.BaseExceptionConstant;
import cn.lionbridgecapital.loan.service.api.IMcrCityOpenCtrlApiService;
import cn.lionbridgecapital.loan.service.api.IMcrPrpsRateApiService;
import cn.lionbridgecapital.loan.service.api.IMcrPrpsRateDtlApiService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.framework.base.context.CurrentThreadContext;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.exception.BaseException;

/**
 * @Description:  客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class McrPrpsRateServiceImpl implements IMcrPrpsRateService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IMcrPrpsRateService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IMcrPrpsRateApiService mcrPrpsRateApiService;

	@Autowired
	private IMcrCityOpenCtrlApiService mcrCityOpenCtrlApiService;

	@Autowired
	private IMcrPrpsRateDtlApiService mcrPrpsRateDtlApiService;

	@Override
	public McrPrpsRateVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		McrPrpsRateDTO dto = this.mcrPrpsRateApiService.selectMcrPrpsRateDTOById(id);
		return McrPrpsRateVOTransform.toVO(dto);
	}

	@Override
	public Long add(McrPrpsRateVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		Long id = this.mcrPrpsRateApiService.addMcrPrpsRate(McrPrpsRateVOTransform.toDTO(entity));
		McrPrpsRateVO vo = this.selectById(id);
		BaseLogUtils.info(logger, "modify", "分级定价新增.新增后数据"+ JSONObject.toJSONString(vo)+"-ID:"+vo.getId() , CurrentThreadContext.getCurrentUserName());
		McrPrpsRateDtlDTO dto = new McrPrpsRateDtlDTO();
		dto.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		dto.setUsrNm(CurrentThreadContext.getCurrentUserName());
		dto.setRateId(id);
		dto.setIsValid(vo.getIsValid());
		dto.setIsDel(vo.getIsDel());
		dto.setCrtTm(new Date());
		dto.setCstTypeCd(vo.getCstTypeCd());
		dto.setCrTypCd(vo.getCrTypCd());
		dto.setCrPd(vo.getCrPd());
		dto.setCrLvlCd(vo.getCrLvlCd());
		dto.setCityCode(vo.getCityCode());
		dto.setAmount(vo.getAmount());
		dto.setIsReduction(vo.getIsReduction());
		dto.setVehicleCrpd(Long.valueOf(vo.getVehicleCrpd()));
		dto.setSLoanNum(Long.valueOf(vo.getsLoanNum()));
		dto.setLoanRatePct(vo.getLoanRatePct());
		mcrPrpsRateDtlApiService.addMcrPrpsRateDtl(dto);
		return id;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.mcrPrpsRateApiService.removeById(id);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(McrPrpsRateVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		McrPrpsRateVO vo = this.selectById(entity.getId());
		BaseLogUtils.info(logger, "modify", "分级定价修改.修改前数据"+ JSONObject.toJSONString(vo)+"-ID:"+entity.getId() , CurrentThreadContext.getCurrentUserName());
		McrPrpsRateDtlDTO dto = new McrPrpsRateDtlDTO();
		dto.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		dto.setUsrNm(CurrentThreadContext.getCurrentUserName());
		dto.setRateId(entity.getId());
		dto.setIsValid(vo.getIsValid());
		dto.setIsDel(vo.getIsDel());
		dto.setMdfTm(new Date());
		dto.setCstTypeCd(vo.getCstTypeCd());
		dto.setCrTypCd(vo.getCrTypCd());
		dto.setCrPd(vo.getCrPd());
		dto.setCrLvlCd(vo.getCrLvlCd());
		dto.setCityCode(vo.getCityCode());
		dto.setAmount(vo.getAmount());
		dto.setIsReduction(vo.getIsReduction());
		dto.setVehicleCrpd(Long.valueOf(vo.getVehicleCrpd()));
		dto.setSLoanNum(Long.valueOf(vo.getsLoanNum()));
		dto.setLoanRatePct(vo.getLoanRatePct());
		boolean flag = this.mcrPrpsRateApiService.modifyMcrPrpsRate(McrPrpsRateVOTransform.toDTO(entity));
		BaseLogUtils.info(logger, "modify", "分级定价修改.修改后数据"+ JSONObject.toJSONString(vo)+"-ID:"+entity.getId() , CurrentThreadContext.getCurrentUserName());
		vo = this.selectById(entity.getId());
		dto.setIsValidLater(vo.getIsValid());
		dto.setMdfTm(new Date());
		dto.setCstTypeCdLater(vo.getCstTypeCd());
		dto.setCrTypCdLater(vo.getCrTypCd());
		dto.setCrPdLater(vo.getCrPd());
		dto.setCrLvlCdLater(vo.getCrLvlCd());
		dto.setCityCodeLater(vo.getCityCode());
		dto.setAmountLater(vo.getAmount());
		dto.setIsReductionLater(vo.getIsReduction());
		dto.setVehicleCrpdLater(Long.valueOf(vo.getVehicleCrpd()));
		dto.setSLoanNumLater(Long.valueOf(vo.getsLoanNum()));
		dto.setLoanRatePctLater(vo.getLoanRatePct());
		mcrPrpsRateDtlApiService.addMcrPrpsRateDtl(dto);
		return flag;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		try{
			if(null == page)page = new Pager();
			page = this.mcrPrpsRateApiService.searchMcrPrpsRateListPage(page);
			if(CollectionUtils.isNotEmpty(page.getResultList())){
				List<McrPrpsRateVO> voList = McrPrpsRateVOTransform.toVOList((List<McrPrpsRateDTO>) page.getResultList());
				for (McrPrpsRateVO vo : voList) {
					if(vo.getCityCode().equals("1111")){
						vo.setCityName("全部");
					}else{
						McrCityOpenCtrlQC mcrCityOpenCtrlQC = new McrCityOpenCtrlQC();
						mcrCityOpenCtrlQC.setIsDel(BaseConstant.IS_YESNO_NO);
						mcrCityOpenCtrlQC.setIsValid(BaseConstant.IS_YESNO_YES);
						mcrCityOpenCtrlQC.setCityCode(vo.getCityCode());
						List<McrCityOpenCtrlDTO> mcrCityOpenCtrlDTOS = mcrCityOpenCtrlApiService.searchMcrCityOpenCtrlDTOList(mcrCityOpenCtrlQC);
						BaseLogUtils.info(logger, "initList", "等级定价.查询城市编码映射. DATA:" + JSONObject.toJSONString(mcrCityOpenCtrlDTOS), CurrentThreadContext.getCurrentUserName());
						if(CollectionUtils.isNotEmpty(mcrCityOpenCtrlDTOS)){
							vo.setCityName(mcrCityOpenCtrlDTOS.get(0).getCityName());
						}
					}
				}
				page.setResultList(voList);
			}
		}catch (BaseException be){
			BaseLogUtils.info(logger,"pushRepaymentPlan","等级定价.异常："+be);
		}catch (Exception e){
			BaseLogUtils.info(logger,"pushRepaymentPlan","等级定价.异常："+e);
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601,"等级定价异常");
		}
		return page;
	}
}
